package com.egg.ylt.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.adapter.ADA_BabyList;
import com.egg.ylt.adapter.ADA_BabyRelation;
import com.egg.ylt.adapter.RvSimpleAdapter;
import com.egg.ylt.pojo.MemberBabyEntity;
import com.egg.ylt.pojo.RelationEntity;
import com.egg.ylt.pojo.RelativeFamilyEntity;
import com.egg.ylt.presenter.impl.PhoneInvitePresenterImpl;
import com.egg.ylt.view.IPhoneInviteView;
import com.yonyou.framework.library.adapter.rv.MultiItemTypeAdapter;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.base.BaseAppCompatActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.DisplayUtil;
import com.yonyou.framework.library.common.utils.ScreenUtil;
import com.yonyou.framework.library.common.utils.ToastUtil;
import com.yonyou.framework.library.dialog.AlertDialog;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by qiuzhenhuan on 2019/1/19.
 */

public class ACT_PhoneInvite extends BaseActivity<PhoneInvitePresenterImpl> implements IPhoneInviteView {

    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.include_iv_back)
    ImageView ivBack;
    @BindView(R.id.include_tv_title)
    TextView tvTitle;
    @BindView(R.id.include_tv_right)
    TextView tvRight;
    @BindView(R.id.tv_relate)
    TextView tvRelate;
    @BindView(R.id.rl_include_title)
    RelativeLayout rlTitle;


    private AlertDialog mRelativeDialog;

    private ADA_BabyRelation mAdapter;

    private String defaultName;
    private String babyId;


    @Override
    protected void getBundleExtras(Bundle extras) {
        babyId = extras.getString("babyId");
        defaultName = extras.getString("defaultName");
    }


    @Override
    protected void initViewsAndEvents() {
        StatusBarUtil.setStatusBarColor(this, R.color.ylt_style_orange);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, rlTitle);

        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setText("邀请家人");
        tvRight.setVisibility(View.GONE);
        StatusBarUtil.setStatusBarColor(this, R.color.color_FFB024);
        StatusBarUtil.StatusBarLightMode(this);
        //动态设置沉浸式状态栏的高度
        int statusBarHeight = ScreenUtil.getStatusBarHeight(mContext);
        rlTitle.setPadding(0, statusBarHeight, 0, rlTitle.getPaddingBottom());

        initSelectRelativeDialog();

        mPresenter.getRelationList();
    }

    private void initSelectRelativeDialog() {

        mRelativeDialog = new AlertDialog.Builder(mContext)
                .setContentView(R.layout.dialog_simple_list)
                .setWidthAndHeight(mScreenWidth - DisplayUtil.dip2px(mContext, 20), ViewGroup.LayoutParams.WRAP_CONTENT)
                .setCancelableOntheOutside(true)
                .create();
        Window window = mRelativeDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        RecyclerView rvBabyList = mRelativeDialog.findViewById(R.id.rv_list);
        rvBabyList.setLayoutManager(new GridLayoutManager(mContext, 3));
        mAdapter = new ADA_BabyRelation(mContext);
        rvBabyList.setAdapter(mAdapter);
        mRelativeDialog.setOnClickListener(R.id.tv_cancel, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRelativeDialog.dismiss();
            }
        });
        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                RelationEntity.ListBean item = mAdapter.getDataList().get(position);
                tvRelate.setText(item.getValue());
                tvRelate.setTag(item.getLabel());
                mRelativeDialog.dismiss();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }


    @Override
    public void showInviteSuccess() {
        ToastUtil.makeText(mContext, "邀请成功", false);
        finish();
    }

    @Override
    public void updateRelationList(List<RelationEntity.ListBean> list) {
        mAdapter.update(list, true);
        if (!StringUtil.isEmpty(defaultName)) {
            for (RelationEntity.ListBean item : list) {
                if (item.getValue().equals(defaultName)) {
                    tvRelate.setText(item.getValue());
                    tvRelate.setTag(item.getLabel());
                    return;
                }
            }
        }
    }


    @OnClick({R.id.rl_relate, R.id.tv_invite, R.id.include_iv_back})
    void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.include_iv_back:
                finish();
                break;
            case R.id.rl_relate:
                mRelativeDialog.show();
                break;
            case R.id.tv_invite:
                String phone = etPhone.getText().toString().trim();
                String relateName = tvRelate.getText().toString().trim();
                String relateId = (String) tvRelate.getTag();

                if (StringUtil.isEmpty(relateName) || relateName.equals("未选择") || StringUtil.isEmpty(relateId)) {
                    ToastUtil.makeText(mContext, "请选择与孩子的关系", false);
                    return;
                }

                if (StringUtil.isEmpty(phone)) {
                    ToastUtil.makeText(mContext, "请输入手机号", false);
                    return;
                }

                if (!StringUtil.isPhone(phone)) {
                    ToastUtil.makeText(mContext, "手机号输入有误", false);
                    return;
                }

                mPresenter.inviteByPhone(phone, babyId, relateId, relateName);
                break;
        }
    }


    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_phone_invite;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    public static void startAc(Context context, String babyId, String defaultName) {
        Intent intent = new Intent(context, ACT_PhoneInvite.class);
        intent.putExtra("babyId", babyId);
        intent.putExtra("defaultName", defaultName);
        context.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mRelativeDialog != null && mRelativeDialog.isShowing()) {
            mRelativeDialog.dismiss();
        }
    }
}
